package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetMenuListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取菜单列表
func NewGetMenuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuListLogic {
	return &GetMenuListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetMenuListLogic) GetMenuList(req *types.GetMenuListRequest) (resp *types.GetMenuListResponse, err error) {
	reply, err := l.svcCtx.AuthRoleRpc.GetMenuList(l.ctx, &v1.GetMenuListRequest{
		Page:       req.Page,
		PageSize:   req.PageSize,
		MenuPid:    req.MenuPid,
		MenuName:   req.MenuName,
		MenuType:   v1.MenuType(req.MenuType),
		Target:     v1.MenuTarget(req.Target),
		Status:     v1.Status(req.Status),
		Identifier: req.Identifier,
		Visible:    v1.MenuVisible(req.Visible),
		Refresh:    v1.MenuRefresh(req.Refresh),
	})
	if err != nil {
		return resp, err
	}

	resp = &types.GetMenuListResponse{ResponsePagination: types.ResponsePagination{
		Page:      reply.GetPagination().GetPage(),
		PageSize:  reply.GetPagination().GetPageSize(),
		TotalPage: reply.GetPagination().GetTotalPage(),
		Total:     reply.GetPagination().GetTotal(),
	}}

	for _, menu := range reply.List {
		resp.List = append(resp.List, types.MenuList{
			Id:           menu.GetId(),
			MenuPid:      menu.GetMenuPid(),
			MenuName:     menu.GetMenuName(),
			MenuType:     int8(menu.GetMenuType()),
			Target:       int8(menu.GetTarget()),
			Url:          menu.GetUrl(),
			Icon:         menu.GetIcon(),
			ListOrder:    menu.GetListOrder(),
			Remark:       menu.GetRemark(),
			Status:       int8(menu.GetStatus()),
			Identifier:   menu.GetIdentifier(),
			Visible:      int8(menu.GetVisible()),
			Refresh:      int8(menu.GetRefresh()),
			CreatedAt:    menu.GetCreatedAt(),
			UpdatedAt:    menu.GetUpdatedAt(),
			MenuLanguage: menu.GetMenuLanguage(),
		})
	}

	return resp, err
}
